import tornado.ioloop
import tornado.web
import pymysql


class MainHandler(tornado.web.RequestHandler):
    def get(self):
        db = pymysql.connect(host="localhost",
                             user="root",
                             password="root",
                             database="xiaoshuo",
                             charset='utf8')
        cursor = db.cursor()
        sql = "select author from authors;"
        cursor.execute(sql)
        test = cursor.fetchall()
        items = []
        i = 1
        for (chap) in test:
           items.append([i,chap[0]])
           i=i+1
        cursor.close()
        db.close()
        self.render("menu.html",  items=items)

class Novels(tornado.web.RequestHandler):
    def get(self):
        self.get_query_argument("w")
        db = pymysql.connect(host="localhost",
                             user="root",
                             password="root",
                             database="xiaoshuo",
                             charset='utf8')

        cursor = db.cursor()
        try:
            query = "SELECT author,novel FROM author_novels WHERE author='{}'".format(self.get_query_argument("w"))
            cursor.execute(query)
        except   Exception as e:
            print(e)
        chapinfo = {}
        para=[]
        for (chap, chapcnt) in cursor:
           chapinfo["author"] = chap
           chapinfo["novel"] = chapcnt
        cursor.close()
        db.close()
        self.render("novels.html",items=chapinfo)

class Novel(tornado.web.RequestHandler):
    def get(self):
        self.get_query_argument("n")
        db = pymysql.connect(host="localhost",
                             user="root",
                             password="root",
                             database="xiaoshuo",
                             charset='utf8')
        cursor = db.cursor()
        chapinfo = []
        sql = "select chapter FROM work_collection WHERE novel='{}'".format(self.get_query_argument("n"))
        sql0 = "SELECT author FROM author_novels WHERE novel='{}'".format(self.get_query_argument("n"))
        novel = self.get_query_argument("n")
        chapinfo.append(novel)
        cursor.execute(sql0)
        for i in cursor:
            author = i[0]
        chapinfo.append(author)
        cursor.execute(sql)
        for chapcnt in cursor:
            chapinfo.append(chapcnt[0])
        cursor.close()
        db.close()
        self.render("novel.html",items=chapinfo)

class ChapterHandler(tornado.web.RequestHandler):
    def get(self):
        self.get_query_argument("c")
        db = pymysql.connect(host="localhost",
                             user="root",
                             password="root",
                             database="xiaoshuo",
                             charset='utf8')

        cursor = db.cursor()
        query = "SELECT chapter,content FROM work_collection WHERE chapter='{}'".format(self.get_query_argument("c"))
        cursor.execute(query)
        chapinfo = {}
        para=[]
        for (chap, chapcnt) in cursor:
           chapinfo["chapter"] = chap
           para=chapcnt.split("\r\n")
           chapinfo["content"] = para
        sql = "SELECT novel FROM work_collection WHERE chapter='{}'".format(self.get_query_argument("c"))
        cursor.execute(sql)
        for i in cursor:
            chapinfo["novel"] = i[0]
        cursor.close()
        db.close()
        self.render("content.html", items=chapinfo)


def make_app():
    return tornado.web.Application([
        (r"/", MainHandler),
        (r"/novel", Novel),
        (r"/novels", Novels),
        (r"/content", ChapterHandler),
    ])


def main():
    app = make_app()
    app.listen(8888)
    tornado.ioloop.IOLoop.current().start()

main()